package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.Notice;
import com.fz.mapper.NoticeMapper;
import com.fz.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存储社区公告发布与状态 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Resource
    NoticeMapper noticeMapper;

    @Override
    public Page<Notice> getList(Integer pageNo, Integer pageSize) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        Page<Notice>noticePage;
        wrapper.orderByDesc("create_time");
        noticePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return noticePage;
    }
    @Override
    public Page<Notice> search(String input, Integer pageNo, Integer pageSize){
        QueryWrapper<Notice> wrapper=new QueryWrapper<>();
        Page<Notice>noticePage;
        wrapper.like("title",input)
                .orderByDesc("create_time");
        noticePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Page<Notice> showList(Integer pageNo, Integer pageSize) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        Page<Notice>noticePage;
        wrapper.orderByDesc("create_time");
        noticePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Boolean add(String title, String content, LocalDateTime time){
        return this.save(new Notice(
                title,content,time
        ));}
    @Override
    public boolean update(Long id,String title,String content){
        return this.updateById(new Notice(
                id,title,content
        ));
    }
    public boolean update(Long id,Long view){
        return this.updateById(new Notice(
                id,view
        ));
    }
    @Override
    public boolean del(Long id,Integer statue,Integer statu){

        return this.updateById(new Notice(id,statue,statu));
    }
    @Override
    public List<Notice> search(String input){
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.like("title", input);
        return this.list(wrapper);

    }
    @Override
    public String detail(Long id){return this.getById(id).getContent();}
    @Override
    public Long getByNoticeId(Long noticeId){//根据id计算浏览量
        return noticeMapper.getByNoticeId(noticeId);
    }
    @Override
    public List<Notice> updateview(List<Notice> noticeList) {
        List<Notice> updatedTNoticeList = new ArrayList<>();

        for (Notice notice : noticeList) {//循环表格里的公告
            Long noticeId = notice.getId();//获取id
            Long View = this.getByNoticeId(noticeId);//计算浏览量

////             view值，我们需要更新这个值
//            UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.eq("id", noticeId);
//            updateWrapper.set("view", View);

            // 如果更新成功，将通知添加到更新列表中
            if (this.update(noticeId,View)) {//如果更新浏览量成功
                updatedTNoticeList.add(notice);//把更新后的公告放到新的表里
                System.out.println("更新浏览量成功，通知ID: " + noticeId);
            } else {
                System.out.println("更新浏览量错误，通知ID: " + noticeId);
            }
        }

        return updatedTNoticeList;
    }
}
